# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:  smart-meter-iot-test
# FileName:     report_data_body.py
# Author  :
# Datetime:     2022/8/8 18:23
# Description：
# -----------------------------------------------------------------------------------
import random
from typing import Dict, Union, Any

# 设备数据（设备定时上传默认5分钟）
# {
# "deviceName": "011234567890", // 表号
# "msg":
# {
# "action”:"report", // 上报数据的action用report
# "iccid": "110", // ICCID卡号
# varchar(32)
# "phase": "1", // 1 - 单项电表，3 - 三相电表
# "status":"on", // 开合闸状态，on、off
# "interval":"5", // 上报间隔分钟数
# "poweryg":"1.00", // 有功总功率
# "poweryga":"1.00", // 有功功率a
# "powerygb":"1.00", // 有功功率b
# "powerygc":"1.00", // 有功功率c
# "powerwg":"1.00", // 无功总功率
# "powerwga":"1.00", // 无功功率a
# "powerwgb":"1.00", // 无功功率b
# "powerwgc":"1.00", // 无功功率c
# "powerrate":"1.00", // 功率因数
# "powerratea":"1.00", // 功率因数a
# "powerrateb":"1.00", // 功率因数b
# "powerratec":"1.00", // 功率因数c
# "frequency”:0.01, // 频率
# "zxyg":"1.00", // 正向有功总电能
# "zxwg":"1.00", // 正向无功总电能
# "fxyg":"", // 反向有功总电能
# "fxwg":"0", // 反向无功总电能
# "zhyg":"0", // 组合有功总电能（用电量，以此作为计费标准)
# "dl":"1.00", // 电流
# "dlb":"", // b相电流
# "dlc”:"", // c相电流
# "dy":"1.00", // 电压
# "dyb":"", // b相电压
# "dyc":"", // c相电压
# "signal":"1" // 信号强度（0 - 31）99, 没信号
# }
# }

from common.util.logUtil.logUtil import LoggerUtil
from core.mqtt.libProject.reportData.meterData import MeterData

logger = LoggerUtil()


def packge_report_data_body(deviceNo, zhyg, zxyg, fxyg, phase, bei) -> Dict[
    str, Union[Dict[Union[str, Any], Union[str, Any]], str]]:
    md = MeterData()
    # 电压
    dy = md.dy(phase)
    # 组合有功增量
    zhygIncrement = md.zhyg_increment(bei)
    # zhygIncrement = random.randint(20, 30)
    # 电流
    dl = md.dl(phase, zhygIncrement, dy)
    # 有功功率
    poweryg = md.power_yg(phase, dy, dl)
    # 无功功率
    powerwg = md.power_reactive()
    # 功率因数
    powerrate = md.power_factor()
    # 频率
    frequency = md.frequency()
    # 信号
    signal = md.signal()
    # 状态
    status = md.status()
    zhyg = round(zhyg + zhygIncrement, 2)
    if phase == 1:
        report_body = {'deviceName': str(deviceNo),
                       'msg': {'action': 'report', 'iccid': '110', 'phase': phase, 'status': status, 'interval': '60',
                               'poweryg': poweryg, 'poweryga': '0', 'powerygb': '0',
                               'powerygc': '0',
                               'powerwg': powerwg, 'powerwga': '0', 'powerwgb': '0',
                               'powerwgc': '0',
                               'powerrate': powerrate, 'powerratea': '0', 'powerrateb': '0',
                               'powerratec': '0',
                               'frequency': frequency, 'zxyg': zhyg, 'zxwg': '0', 'fxyg': fxyg,
                               'fxwg': '0',
                               'zhyg': zhyg, 'dl': dl, 'dlb': '0', 'dlc': '0',
                               'dy': dy, 'dyb': '0',
                               'dyc': '0',
                               'signal': signal}}
    elif phase == 3:
        report_body = {'deviceName': str(deviceNo),
                       'msg': {'action': 'report', 'iccid': '110', 'phase': phase, 'status': status, 'interval': '60',
                               'poweryg': poweryg[0], 'poweryga': poweryg[1], 'powerygb': poweryg[2],
                               'powerygc': poweryg[3],
                               'powerwg': powerwg, 'powerwga': powerwg, 'powerwgb': powerwg,
                               'powerwgc': powerwg,
                               'powerrate': powerrate, 'powerratea': powerrate, 'powerrateb': powerrate,
                               'powerratec': powerrate,
                               'frequency': frequency, 'zxyg': zhyg, 'zxwg': '0', 'fxyg': fxyg,
                               'fxwg': '0',
                               'zhyg': zhyg, 'dl': dl[0], 'dlb': dl[1], 'dlc': dl[2],
                               'dy': dy[0], 'dyb': dy[1], 'dyc': dy[2],
                               'signal': signal}}
    else:
        print(deviceNo, phase)
        logger.error(f"未获取到电表类型")
        raise f"未获取到电表类型"
    print(f"上报电表:{deviceNo}:上报电量:{zhyg},增量:{zhygIncrement}")
    return report_body


def packge_reprt_collector_body(deviceName, ):
    report_body = {
        # 基站编号
        "deviceName": deviceName,
        "msg": {
            # 上报数据的action用collectorReport
            "action": "collectorReport",
            # ICCID卡号，20位字符
            "iccid": "110",
            # 基站型号, 2位字符
            "type": "13",
            # 基站版本号，6位字符
            "version": "123456",
            # 运行状态
            "status": "1",
            # 上报间隔分钟数
            "interval": "5",
            # 基站温度，2位字符，无小数
            "temperature": "25",
            # 信号强度（0 - 31），99表示没信号
            "signal": random.randint(1, 35),
        }
    }
    return report_body


if __name__ == '__main__':
    print(packge_report_data_body(1, 5, 5, 0, 3, 30))
